<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="resources/shadow-dom.js"></script>
</head>
<body>
<div id="console">
</div>
<script>
description("This tests the blur() on on shadow host should work when a shadow host contains a focused element in its shadow DOM subtrees property (bug 81102)");

if (!window.internals)
    debug('This test runs on DRT only');

document.body.appendChild(
    createDOM('div', {'id':'shadowHostA'},
        createShadowRoot(
            createDOM('p', {'id':'childInTreeOne', 'tabindex':'1'}),
            createDOM('div', {'id':'shadowHostB'},
                createShadowRoot(
                    createDOM('p', {'id':'childInTreeTwo', 'tabindex':'2'}))))));

document.body.appendChild(
    createDOM('div', {'id':'shadowHostC'},
        createShadowRoot(
            createDOM('p', {'id':'childInTreeThree', 'tabindex':'3'})))
);

getNodeInComposedTree('shadowHostA').onblur = function() { debug("shadowHostA onblur called!"); }
getNodeInComposedTree('shadowHostA/shadowHostB').onblur = function() { debug("shadowHostB onblur called!"); }

// childInTreeOne foucsed - shadowHostA blurred
evalAndLog("getNodeInComposedTree('shadowHostA/childInTreeOne').focus()");
shouldBeEqualToString("document.activeElement.id", "shadowHostA");
evalAndLog("getNodeInComposedTree('shadowHostA').blur();");
shouldBe("document.activeElement", "document.body");
shouldBe("getNodeInComposedTree('shadowHostA/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostA/shadowHostB/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostC/').activeElement", "null");

// childInTreeTwo foucsed - shadowHostB blurred
evalAndLog("getNodeInComposedTree('shadowHostA/shadowHostB/childInTreeTwo').focus()");
shouldBeEqualToString("document.activeElement.id", "shadowHostA");
evalAndLog("getNodeInComposedTree('shadowHostA/shadowHostB').blur();");
shouldBe("document.activeElement", "document.body");
shouldBe("getNodeInComposedTree('shadowHostA/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostA/shadowHostB/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostC/').activeElement", "null");

// childInTreeTwo foucsed - shadowHostA blurred
evalAndLog("getNodeInComposedTree('shadowHostA/shadowHostB/childInTreeTwo').focus()");
shouldBeEqualToString("document.activeElement.id", "shadowHostA");
evalAndLog("getNodeInComposedTree('shadowHostA').blur();");
shouldBe("document.activeElement", "document.body");
shouldBe("getNodeInComposedTree('shadowHostA/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostA/shadowHostB/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostC/').activeElement", "null");

// childInTreeThree foucsed - shadowHostA blurred
evalAndLog("getNodeInComposedTree('shadowHostC/childInTreeThree').focus()");
shouldBeEqualToString("document.activeElement.id", "shadowHostC");
evalAndLog("getNodeInComposedTree('shadowHostA').blur();");
shouldBeEqualToString("document.activeElement.id", "shadowHostC");
shouldBe("getNodeInComposedTree('shadowHostA/').activeElement", "null");
shouldBe("getNodeInComposedTree('shadowHostA/shadowHostB/').activeElement", "null");
shouldBeEqualToString("getNodeInComposedTree('shadowHostC/').activeElement.id", "childInTreeThree");

var successfullyParsed = true;
</script>
</body>
</html>
